//
//  XHFeedBackViewController.m
//  xinhua
//
//  Created by 冷山 on 15/11/11.
//  Copyright © 2015年 lexingsoft. All rights reserved.
//

#import "XHFeedBackViewController.h"
#import "Services.h"

#define Width                        ([[UIScreen mainScreen] bounds].size.width)
#define Height                       ([[UIScreen mainScreen] bounds].size.height)

const int number = 140;

@interface XHFeedBackViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *ui_textView;
@property (weak, nonatomic) IBOutlet UILabel *ui_moRenLabel;
@property (weak, nonatomic) IBOutlet UILabel *ui_onlyLabel;
@property (strong, nonatomic) UIButton * ui_finishBtn;

@end

@implementation XHFeedBackViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleBordered
                                                                   target:self
                                                                   action:@selector(finish:)];
    self.navigationItem.rightBarButtonItem = rightBarBtn;

    [self createUI];
}

- (void)createUI
{
    self.ui_textView.backgroundColor = [UIColor whiteColor];
    self.ui_textView.layer.masksToBounds = YES;
    self.ui_textView.layer.cornerRadius = 8;
//    self.ui_textView.layer.borderColor = [ColorManager colorWithHexString:@"#DEDEDE"].CGColor;
//    self.ui_textView.layer.borderWidth = 1;
    self.ui_textView.delegate = self;
    [self.ui_textView resignFirstResponder];
    
    self.ui_onlyLabel.text = [NSString stringWithFormat:@"0/140字"];
    
}

- (void)finish:(UIButton*)btn
{
    if ([self canCommit])
    {
        if (self.bookId) {
            [ServiceManager writeBookReviewWithComment:self.ui_textView.text book:self.bookId orderSn:self.orderSn targetId:self.targetId success:^(NSDictionary *data)
             {
                 [PromptAction showSuccessPrompt:@"亲,您的评价已经送达~"];
                 [self.navigationController popViewControllerAnimated:YES];
             } failure:^{
             }];
        }else
        {
            [ServiceManager sendReviewWithComment:[NSString stringWithFormat:@"%@",self.ui_textView.text] success:^(NSDictionary *data)
            {
                [PromptAction showSuccessPrompt:@"亲,您的意见已经送达~"];
                [self.navigationController popViewControllerAnimated:YES];
            } failure:^{
            }];
        }
    
    }

}

- (BOOL)canCommit
{
    int deltValue = (int)(number - self.ui_textView.text.length);
    
    if ([NSString checkNullString:self.ui_textView.text])
    {
        [PromptAction showErrorPrompt:@"内容不能为空"];
        return NO;
    }
    if (deltValue< 0)
    {
        [PromptAction showErrorPrompt:[NSString stringWithFormat:@"内容不能超过%d个字",number]];
        return NO;
    }
    return YES;
 }


#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView
{
    NSInteger lenght;
    
    lenght = self.ui_textView.text.length;
    self.ui_onlyLabel.text = [NSString stringWithFormat:@"%ld/140字",lenght];
    
    
    if (self.ui_textView.text.length > number)
    {
        self.ui_onlyLabel.textColor = [ColorManager colorWithHexString:@"E8443A"];
    }
    else if (self.ui_onlyLabel.text.length <= number)
    {
        self.ui_onlyLabel.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.6];
    }
    
    
    if ([self.ui_textView.text isEqualToString:@""])
    {
         [self.ui_moRenLabel setHidden:NO];
    }
    else
    {
        [self.ui_moRenLabel setHidden:YES];
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    [super touchesBegan:touches withEvent:event];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
